package domain;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import util.HibernateUtil;

public class DAOContactGroup {
	Session session = null;
	private static DAOContactGroup dao=null;
	
	public DAOContactGroup(){				
		session = HibernateUtil.getSessionFactory().openSession();
	}
	
	public static DAOContactGroup getInstance(){
		if(dao==null)
			dao=new DAOContactGroup();
		return dao;
	}
	
	public ContactGroup addGroupName(String name){
		try{
			//begin a transaction
			org.hibernate.Transaction tx = session.beginTransaction();
			//create a contact and save it into the DB
			ContactGroup contactGroup = new ContactGroup();
			contactGroup.setGroupName(name);

			//save the contact into the DB
			session.save(contactGroup); // or session.persist(contact);
			
			tx.commit();
			return contactGroup;
			}catch(Exception e){
						System.out.println(e.getMessage());
			}
			return null;
	}
	
	public List<ContactGroup> getContactGroups(){
		
		Query q=session.createQuery("from ContactGroup");
		List<ContactGroup> result=q.list();
		return result;
	}
}
